using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;
using NPOI.POIFS.FileSystem;
using System.Data.Common;
using System.Text;

namespace JNPF.Basics.Logic
{
    public class PointswithdrawalService : DataFactory, PointswithdrawalIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<PointswithdrawalEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<PointswithdrawalEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<PointswithdrawalListOutput>> GetPageList(PointswithdrawalListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"SELECT
	                        poin.* ,
	                        u.F_REALNAME userName
                        FROM
	                        JIUSHI_POINTSWITHDRAWAL poin LEFT JOIN base_user u ON poin.F_UserId = u.F_ID
	                        where 1=1 ");

                //查询手机号
                if (!string.IsNullOrEmpty(input.userName))
                {

                    dbSql.Append(" AND   u.F_REALNAME like  @userName");
                    parameters.Add(DataHelper.CreateParameter("@userName", "%" + input.userName + "%"));
                }

                if (!input.IsAdmin)
                {
                    //自己只能看到自己的
                    dbSql.Append("  and poin.F_UserId =  @userId");
                    parameters.Add(DataHelper.CreateParameter("@userId", UserProvider.Instance.Get().Id));
                }
                return await PageHelper.GetPageResultAsync<PointswithdrawalListOutput>(db, input, dbSql.ToString(), parameters.ToArray());

            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(PointswithdrawalEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 提现
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="userextendEntity"></param>
        /// <param name="partnerPointsdetails"></param>
        /// <returns></returns>
        public async Task Create(PointswithdrawalEntity entity, UserextendEntity userextendEntity, PointsdetailsEntity partnerPointsdetails)
        {

            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.InsertAsync(entity);

                await db.UpdateAsync(userextendEntity);

                await db.InsertAsync(partnerPointsdetails);

                await db.CommitTransactionAsync();
            }
        }



        /// <summary>
        /// 提现审核
        /// </summary>
        /// <param name="pointswithdrawalEntity">提现表数据</param>
        /// <param name="userextendEntity">用户数据</param>
        /// <param name="partnerPointsdetails">积分表数据</param>
        /// <returns></returns>
        public async Task Examine(PointswithdrawalEntity pointswithdrawalEntity, UserextendEntity userextendEntity, PointsdetailsEntity partnerPointsdetails)
        {

            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();


                await db.UpdateAsync(pointswithdrawalEntity);

                await db.UpdateAsync(userextendEntity);

                if (partnerPointsdetails != null)
                {
                    await db.InsertAsync(partnerPointsdetails);
                }
                await db.CommitTransactionAsync();
            }
        }



        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(PointswithdrawalEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(PointswithdrawalEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<PointswithdrawalEntity>> GetList(PointswithdrawalListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<PointswithdrawalEntity>();
                //查询用户Id
                if (!string.IsNullOrEmpty(input.userId))
                {
                    queryWhere = queryWhere.And(m => m.UserId.Contains(input.userId));
                }
                //查询类别：0 待审核 1 提现成功 2 拒绝
                if (input.type.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Type == input.type);
                }
                //查询备注
                if (!string.IsNullOrEmpty(input.remark))
                {
                    queryWhere = queryWhere.And(m => m.Remark.Contains(input.remark));
                }
                //查询积分
                if (input.points.ToDecimal() > 0)
                {
                    queryWhere = queryWhere.And(m => m.Points == input.points);
                }
                ////查询排序
                //if (!string.IsNullOrEmpty(input.sortCode))
                //{
                //	queryWhere = queryWhere.And(m => m.SortCode.Contains(input.sortCode));
                //}
                return await db.GetIQueryable<PointswithdrawalEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}